package com.maotai.llzhou.util;


import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class IPFactory {
	
	public static String HTTP_TYPE="JL";
	
	private static String QW_IP_URL = "http://dynamic.goubanjia.com/dynamic/get/6d32d7416b96740e2dcbe22e858f4d51.html?ramdom=yes";
	private static String KD_IP_URL = "http://dps.kuaidaili.com/api/getdps/?orderid=991312549100574&num=100&ut=1&format=json&sep=1";
	public static String KD_IP_URL5 = "http://dps.kuaidaili.com/api/getdps/?orderid=991312549100574&num=5&ut=1&format=json&sep=1";
	private static String KD_IP_URL_SINGLE = "http://dps.kuaidaili.com/api/getdps/?orderid=991312549100574&num=1&ut=1&format=json&sep=1";
	private static String TY_IP_URL = "http://http-api.taiyangruanjian.com/getip?num=1&type=1&pro=&city=0&yys=0&port=11&pack=9265&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=3&regions=";
	public static String TY_IP_URL_5 = "http://http-api.taiyangruanjian.com/getip?num=5&type=2&pro=&city=0&yys=0&port=11&pack=9265&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String TY_IP_URL_10 = "http://http-api.taiyangruanjian.com/getip?num=10&type=2&pro=&city=0&yys=0&port=11&pack=9265&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
//	public static String TY_IP_URL_20 = "http://http-api.taiyangruanjian.com/getip?num=20&type=2&pro=&city=0&yys=0&port=11&pack=9265&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String TY_IP_URL_100 = "http://http-api.taiyangruanjian.com/getip?num=100&type=2&pro=&city=0&yys=0&port=11&pack=9265&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String TY_IP_URL_500 = "http://http-api.taiyangruanjian.com/getip?num=500&type=2&pro=&city=0&yys=0&port=11&pack=9265&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
//	public static String ZM_IP_SOCKET_URL_200 = "http://webapi.http.zhimacangku.com/getip?num=200&type=2&pro=&city=0&yys=0&port=2&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String ZM_IP_SOCKET_URL_200 = "http://webapi.http.zhimacangku.com/getip?num=100&type=2&pro=&city=0&yys=0&port=2&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String ZM_IP_SOCKET_URL_1 = "http://webapi.http.zhimacangku.com/getip?num=1&type=2&pro=&city=0&yys=0&port=2&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String ZM_IP_HTTPS_URL_1 = "http://webapi.http.zhimacangku.com/getip?num=1&type=2&pro=&city=0&yys=0&port=11&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String ZM_IP_HTTPS_URL_20 = "http://webapi.http.zhimacangku.com/getip?num=20&type=2&pro=&city=0&yys=0&port=11&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String ZM_IP_HTTPS_URL_50 = "http://webapi.http.zhimacangku.com/getip?num=50&type=2&pro=&city=0&yys=0&port=11&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String ZM_IP_HTTPS_URL_100 = "http://webapi.http.zhimacangku.com/getip?num=100&type=2&pro=&city=0&yys=0&port=11&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
	public static String ZM_IP_HTTPS_URL_200 = "http://webapi.http.zhimacangku.com/getip?num=200&type=2&pro=&city=0&yys=0&port=11&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
//	public static String ZM_IP_HTTPS_URL_200 = "http://ip.16yun.cn:817/myip/pl/14d6a0ad-25d2-4e52-a905-8f1b4f8faa57/?s=afgulrahgw&u=vipwangtao888&format=json";
	public static String TY_IP_URL_20 = ZM_IP_HTTPS_URL_20;
	public static String KD_IP_URL_50 = "http://svip.kuaidaili.com/api/getproxy/?orderid=901462514542714&num=50&b_pcchrome=1&b_pcie=1&b_pcff=1&protocol=2&method=2&an_an=1&an_ha=1&format=json&sep=1";
//	public static String XM_IP_HTTP_URL_50="http://www.xiongmaodaili.com/xiongmao-web/api/glip?secret=599f1d47e9bd54e18dc95a2ad9881e0a&orderNo=GL201806061702158RwrArPj&count=50&isTxt=0";
	public static String XM_IP_HTTP_URL_50="http://www.xiongmaodaili.com/xiongmao-web/api/glip?secret=599f1d47e9bd54e18dc95a2ad9881e0a&orderNo=GL201806061702158RwrArPj&count=50&isTxt=0";public static String JL_IP_HTTP_URL_50="http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=50&time=100&pro=&city=&port=1&format=json&ss=5&css=&dt=1";
	//public static String BY_IP_HTTP_URL_200 = "http://baiyedaili.cn:8088/api.aspx?dno=maotaijiu";
	public static String BY_IP_HTTP_URL_200 = "http://baiyedaili.cn:8088/api.aspx?dno=maotaijiu";
	public static String JL_IP_HTTP_URL_1_WT = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=1&time=1&pro=&city=&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
//	public static String JL_IP_HTTP_URL_200 = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=199&time=1&pro=&city=&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
	public static String JL_IP_HTTP_URL_200_WT = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=200&time=100&pro=&city=&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
	
	public static String NM_IP_HTTP_URL_200 = "http://webapi.niumoip.com/v1/http/agent/ip/get?name=quzong&u=AWJVP1NnVitaKAVyBnxSa1puU2U=&number=1&region=&type=1&ts=2&lb_type=1";
	
	//周亮亮 精灵
	public static String JL_IP_HTTP_URL_1 = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=1&time=101&pro=&city=&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
	public static String JL_IP_HTTP_URL_5 = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=5&time=1&pro=&city=&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
//	public static String JL_IP_HTTP_URL_200 = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=199&time=1&pro=&city=&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
	public static String JL_IP_HTTP_URL_200 = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=200&time=100&pro=&city=&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
		
		
	
	public static String getIpByQuanWang() {
		return getIpByUrl(QW_IP_URL);
	}

	public static String getIpByTY() {
		return getIpByUrl(TY_IP_URL);
	}

	private static String getIpByUrl(String url) {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpGet httpGet = new HttpGet(url);
		String proxyIpAndPort = "localhost:8080";
		try {
			CloseableHttpResponse response = closeableHttpClient
					.execute(httpGet);
			// getEntity()
			HttpEntity httpEntity = response.getEntity();
			if (httpEntity != null) {
				proxyIpAndPort = EntityUtils.toString(httpEntity, "UTF-8");
				System.out.println("response:" + proxyIpAndPort);
			}
			closeableHttpClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return proxyIpAndPort;
	}
	public static JSONArray getIpAndPort() {
		JSONArray jArray = new JSONArray();
		if("BY".equals(HTTP_TYPE)){
			HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
			// HttpClient
			CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
			HttpGet httpGet = new HttpGet(BY_IP_HTTP_URL_200);
			String proxyIpAndPort = "localhost:8080";
			String[] strs = null;
			try {
				while (true) {
					CloseableHttpResponse response = closeableHttpClient
							.execute(httpGet);
					// getEntity()
					HttpEntity httpEntity = response.getEntity();
					if (httpEntity != null) {
						proxyIpAndPort = EntityUtils.toString(httpEntity, "UTF-8");
						System.out.println("response:" + proxyIpAndPort);
						strs = proxyIpAndPort.split("\r\n");
					}
					if (proxyIpAndPort.contains(":")) {
						break;
					}
					Thread.sleep(500);
				}
				closeableHttpClient.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			if(strs!=null && strs.length>0){
				for(String str:strs){
					String[] ips = str.split(":");
					JSONObject jo = new JSONObject();
					jo.put("ip", ips[0]);
					jo.put("port", ips[1]);
					jArray.add(jo);
				}
			}
		}else if("JL".equals(HTTP_TYPE)){
			JSONObject jObject = IPFactory
					.getKdByUrl(IPFactory.JL_IP_HTTP_URL_200);
			JSONArray jArray2 = jObject.getJSONArray("data");
			for (int i = 0; i < jArray2.size(); i++) {
				JSONObject jj = (JSONObject) jArray2.get(i);
				JSONObject jo = new JSONObject();
				jo.put("ip", jj.getString("IP"));
				jo.put("port", jj.getIntValue("Port"));
				jArray.add(jo);
			}
		}
		else if("NM".equals(HTTP_TYPE)){
			JSONObject jObject = IPFactory
					.getKdByUrl(IPFactory.NM_IP_HTTP_URL_200);
			JSONArray jArray2 = jObject.getJSONArray("data");
			for (int i = 0; i < jArray2.size(); i++) {
				JSONObject jj = (JSONObject) jArray2.get(i);
				JSONObject jo = new JSONObject();
				jo.put("ip", jj.getString("ip"));
				jo.put("port", jj.getIntValue("port"));
				jArray.add(jo);
			}
		}
		return jArray;
	}
	public static String[] getBYByUrl(String url) {
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpGet httpGet = new HttpGet(url);
		String proxyIpAndPort = "localhost:8080";
		String[] strs = null;
		try {
			while (true) {
				CloseableHttpResponse response = closeableHttpClient
						.execute(httpGet);
				// getEntity()
				HttpEntity httpEntity = response.getEntity();
				if (httpEntity != null) {
					proxyIpAndPort = EntityUtils.toString(httpEntity, "UTF-8");
					System.out.println("response:" + proxyIpAndPort);
					strs = proxyIpAndPort.split("\r\n");
				}
				if (proxyIpAndPort.contains(":")) {
					break;
				}
				Thread.sleep(500);
			}
//			CloseableHttpResponse response = closeableHttpClient
//						.execute(httpGet);
//			// getEntity()
//			HttpEntity httpEntity = response.getEntity();
//			if (httpEntity != null) {
//					proxyIpAndPort = EntityUtils.toString(httpEntity, "UTF-8");
//					System.out.println("response:" + proxyIpAndPort);
//					strs = proxyIpAndPort.split("\r\n");
//				Thread.sleep(500);
//			}
			closeableHttpClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strs;
	}
	public static JSONObject getKdByUrl() {
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		JSONObject jObject = new JSONObject();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpGet httpGet = new HttpGet(KD_IP_URL);
		String proxyIpAndPort = "localhost:8080";
		try {
			while (true) {
				CloseableHttpResponse response = closeableHttpClient
						.execute(httpGet);
				// getEntity()
				HttpEntity httpEntity = response.getEntity();
				if (httpEntity != null) {
					proxyIpAndPort = EntityUtils.toString(httpEntity, "UTF-8");
					System.out.println("response:" + proxyIpAndPort);
				}

				jObject = JSONObject.parseObject(proxyIpAndPort);
				if (jObject.getIntValue("code") == 0) {
					break;
				}
			}
			closeableHttpClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jObject;
	}

	public static JSONObject getKdByUrl(String url) {
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		JSONObject jObject = new JSONObject();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpGet httpGet = new HttpGet(url);
		String proxyIpAndPort = "localhost:8080";
		try {
			while (true) {
				CloseableHttpResponse response = closeableHttpClient
						.execute(httpGet);
				// getEntity()
				HttpEntity httpEntity = response.getEntity();
				if (httpEntity != null) {
					proxyIpAndPort = EntityUtils.toString(httpEntity, "UTF-8");
					System.out.println("response:" + proxyIpAndPort);
				}
				if(proxyIpAndPort.contains("503")||proxyIpAndPort.contains("500")) {
					Thread.sleep(1000);
					continue;
				}
				jObject = JSONObject.parseObject(proxyIpAndPort);
				if (jObject.getIntValue("code") == 0) {
					break;
				}else if (jObject.getString("msg").contains("为空")) {
					break;
				}
				break;
//				Thread.sleep(1000);
			}
			closeableHttpClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jObject;
	}

	public static JSONObject getKdByUrlSingle() {
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		JSONObject jObject = new JSONObject();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpGet httpGet = new HttpGet(KD_IP_URL_SINGLE);
		String proxyIpAndPort = "localhost:8080";
		try {
			while (true) {
				CloseableHttpResponse response = closeableHttpClient
						.execute(httpGet);
				// getEntity()
				HttpEntity httpEntity = response.getEntity();
				if (httpEntity != null) {
					proxyIpAndPort = EntityUtils.toString(httpEntity, "UTF-8");
					System.out.println("response:" + proxyIpAndPort);
				}
				jObject = JSONObject.parseObject(proxyIpAndPort);
				if (jObject.getIntValue("code") == 0) {
					break;
				}
			}
			closeableHttpClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jObject;
	}

	public static RequestConfig getRequestConfig() {
		// 增加代理
		String[] ipAndPort = null;
		while (true) {
			String proxyIpAndPort = IPFactory.getIpByQuanWang();
			ipAndPort = proxyIpAndPort.trim().replaceAll("\r|\n", "")
					.split(":");
			if (ipAndPort != null && !"".equals(proxyIpAndPort)
					&& ipAndPort.length == 2) {
				// 证明获取到的ip有问题
				System.out.println(proxyIpAndPort);
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
		// 第一次访问login的时候加上cookie，防止滑块的时候有其他验证
		// 增加代理
		HttpHost proxy = new HttpHost(ipAndPort[0],
				Integer.parseInt(ipAndPort[1]));
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(3000000).setConnectTimeout(3000000)
				.setConnectionRequestTimeout(3000000).setProxy(proxy).build();
		return requestConfig;
	}

	public static RequestConfig getRequestConfigTY() {
		// 增加代理
		String[] ipAndPort = null;
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}
		while (true) {
			String proxyIpAndPort = IPFactory.getIpByTY();
			ipAndPort = proxyIpAndPort.trim().replaceAll("\r|\n", "")
					.split(":");
			if (ipAndPort != null && !"".equals(proxyIpAndPort)
					&& ipAndPort.length == 2) {
				// 证明获取到的ip有问题
				System.out.println(proxyIpAndPort);
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
		// 第一次访问login的时候加上cookie，防止滑块的时候有其他验证
		// 增加代理
		HttpHost proxy = new HttpHost(ipAndPort[0],
				Integer.parseInt(ipAndPort[1]));
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(3000000).setConnectTimeout(3000000)
				.setConnectionRequestTimeout(3000000).setProxy(proxy).build();
		return requestConfig;
	}

	public static RequestConfig getKdSingleRequestConfig() {
		JSONObject jobject = getKdByUrlSingle();
		System.out.println(jobject);
		JSONObject data = jobject.getJSONObject("data");
		if (data == null) {
			return null;
		}
		// {"msg": "", "code": 0, "data": {"count": 10, "proxy_list":
		// ["124.172.117.189:19812", "219.133.31.120:26947",
		// "183.237.194.145:28436", "183.62.172.50:23485",
		// "163.125.157.243:17503", "183.57.42.79:26483",
		// "202.103.150.70:17251", "182.254.129.124:15395",
		// "58.251.132.181:20659", "112.95.241.76:21948"]}}
		JSONArray jArray = data.getJSONArray("proxy_list");
		String ipAndPorts = jArray.get(0) + "";
		String[] ipAndPort = ipAndPorts.split(":");
		if (ipAndPort == null || ipAndPort.length < 2) {
			return null;
		}
		/*
		 * { "msg": "", "code": 0, "data": { "count": 10, "proxy_list": [
		 * "124.172.117.189:19812", "219.133.31.120:26947",
		 * "183.237.194.145:28436", "183.62.172.50:23485",
		 * "163.125.157.243:17503", "183.57.42.79:26483",
		 * "202.103.150.70:17251", "182.254.129.124:15395",
		 * "58.251.132.181:20659", "112.95.241.76:21948" ] } }
		 */
		// 增加代理
		HttpHost proxy = new HttpHost(ipAndPort[0],
				Integer.parseInt(ipAndPort[1]));
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(300000).setConnectTimeout(300000)
				.setConnectionRequestTimeout(300000).setProxy(proxy).build();
		return requestConfig;
	}
	
	public static RequestConfig getKdRequestConfigNoProxy() {
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(5000).setConnectTimeout(5000)
				.setConnectionRequestTimeout(5000)
				.build();
		return requestConfig;
	}

	public static RequestConfig getKdRequestConfig(String ipAndPorts) {
		String[] ipAndPort = ipAndPorts.trim().split(":");
		if (ipAndPort == null || ipAndPort.length < 2) {
			return null;
		}
		/*
		 * { "msg": "", "code": 0, "data": { "count": 10, "proxy_list": [
		 * "124.172.117.189:19812", "219.133.31.120:26947",
		 * "183.237.194.145:28436", "183.62.172.50:23485",
		 * "163.125.157.243:17503", "183.57.42.79:26483",
		 * "202.103.150.70:17251", "182.254.129.124:15395",
		 * "58.251.132.181:20659", "112.95.241.76:21948" ] } }
		 */
		// 增加代理
		HttpHost proxy = new HttpHost(ipAndPort[0],
				Integer.parseInt(ipAndPort[1]));
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(20000).setConnectTimeout(5000)
				.setConnectionRequestTimeout(5000)
				.setProxy(proxy)
				.build();
		return requestConfig;
	}

	public static RequestConfig getRequestConfigShort() {
		// 增加代理
		String[] ipAndPort = null;
		while (true) {
			String proxyIpAndPort = IPFactory.getIpByQuanWang();
			ipAndPort = proxyIpAndPort.trim().replaceAll("\r|\n", "")
					.split(":");
			if (ipAndPort != null && !"".equals(proxyIpAndPort)
					&& ipAndPort.length == 2) {
				// 证明获取到的ip有问题
				System.out.println(proxyIpAndPort);
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
		// 第一次访问login的时候加上cookie，防止滑块的时候有其他验证
		// 增加代理
		HttpHost proxy = new HttpHost(ipAndPort[0],
				Integer.parseInt(ipAndPort[1]));
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(30000).setConnectTimeout(30000)
				.setConnectionRequestTimeout(30000).setProxy(proxy).build();
		return requestConfig;
	}

	public static void main(String[] args) {
		JSONArray jArray = IPFactory.getIpAndPort();
		System.out.println(jArray);
	}
}
